﻿using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using AutoMapper;
using Ninject;
using Ninject.Web.Common;
using SuCasa.Infrastructure;
using SuCasa.Models;

namespace SuCasa
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : NinjectHttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "AssignmentList",
                "{controller}/{action}/{memberId}/{courseId}",
                new { controller = "Assignment", action = "List", id = UrlParameter.Optional}
                );


        }

        private IKernel kernel = new StandardKernel();

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected override IKernel CreateKernel()
        {
            kernel.Load(Assembly.GetExecutingAssembly());
            Mapper.CreateMap<MemberEntryViewModel, Member>();
            Mapper.CreateMap<Member, MemberEntryViewModel>();
            Mapper.CreateMap<CourseViewModel, Course>();
            Mapper.CreateMap<Member, MemberDetailsViewModel>();
            Mapper.CreateMap<Member, MemberAddEntryViewModel>();
            Mapper.CreateMap<MemberAddEntryViewModel, Member>();
            Mapper.CreateMap<CallLogViewModel, CallLog>();
            Mapper.CreateMap<CallLog, CallLogViewModel>();
            //Mapper.CreateMap<, Class>();
            return kernel;
        }

        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}